web browserから任意のデータをdownloadするscript
ブラウザからファイル出力(ダウンロード処理)にあることをほぼそのままmodule化した
一旦Blobを経由し、data URIを作る
code:script.js
export function downloadObject(object) {
// download dataを作成
const blob = new Blob(JSON.stringify(object), {type: 'octet/stream'});
// download linkを生成
const url = URL.createObjectURL(blob);
// 隠しa要素を使ってdownloadする
const a = document.createElement('a');
a.href = url;
a.download = 'import.json';
a.style.display = 'none';
// downloadを実行
a.click();
// 後始末
URL.revokeObjectURL(url);
}
#2021-03-01 17:09:50
#2021-01-25 00:22:32